// Justin Cowen
// CSC 332 - Data Structures
// February 12, 2010
// Program #2
// This program uses Linked Lists and the various methods associated with it
// to print the list, reverse the list, trim the list, and remove items from the list

import java.util.Iterator;

class Prog2 {        //main class
    public static void main(String[] args){
        ExtendedLinkedList<Integer> list = new ExtendedLinkedList<Integer>( );
        ExtendedLinkedList<Integer> list2 = new ExtendedLinkedList<Integer>();
        ExtendedLinkedList<Integer> list3 = new ExtendedLinkedList<Integer>();
        list.add(0); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6);
        System.out.println("The original list: ");
        list.print1(list);
        System.out.println("\nA Test of the reverse method: ");
        list2 = list.reverse(list);
        list2.print1(list2);
        System.out.println("\nA Test of the trim method: ");
        list3 = list.trim(list);
        list3.print1(list3);
        System.out.println("\nA Test of the remove method (removing elements 2-4): ");      
        list.remove(list, 2, 4);
        list.print1(list);
        System.out.println("\nA Test of the second constructor, passing an Integer array with numbers 0-4");
        Integer[] array = new Integer[5]; //array to pass constructor
        array[0]=0; array[1]=1; array[2]=2; array[3]=3; array[4]=4; 
        ExtendedLinkedList<Integer> test1 = new ExtendedLinkedList(array);
        test1.print1(test1); 
    }
}

class ExtendedLinkedList<T> extends MyLinkedList<T>{
	
    public ExtendedLinkedList(){    //default constructor
        super();
    }

    public ExtendedLinkedList(T[]x){ //second constructor
        super();
        for(int i=0; i<x.length; i++){
            add(x[i]);
        }
    }
    static <T> void print1(ExtendedLinkedList<T> list){  // print statement using enhanced for loop
        for(T item:list)
            System.out.print(item+" ");
        System.out.print("\n");
	}

    static <T> void print2(ExtendedLinkedList<T> list){ // print statement using iterator
        Iterator<T> itr = list.iterator();
        while(itr.hasNext())
        {
            T item = itr.next();
            System.out.print(item+" ");
        }
        System.out.print("\n");
    }
    
    static <T> ExtendedLinkedList<T> reverse(ExtendedLinkedList<T> list){ //method reverse the passed list
        ExtendedLinkedList<T> toReturn = new ExtendedLinkedList<T>(); //new list containing reverse
        int Size = list.size();
        for(int i = Size-1; i>=0; i--){
            toReturn.add(list.get(i));   		
        }
        return toReturn;
    }
	
    static <T> ExtendedLinkedList<T> trim(ExtendedLinkedList<T> list){ //trims the first and last elements in the list
        ExtendedLinkedList<T> toReturn = new ExtendedLinkedList<T>(); //new list containing trim
        int Size = list.size();
        for(int i = 1; i<Size-1; i++){
            toReturn.add(list.get(i));
        }
        return toReturn;
    }
    
    static <T> void remove(ExtendedLinkedList<T> list, int beg, int end){//removes all elements between beg and end
        int start=0;    //NOTE: integers passed to method assumed to be taken from zero, 
        int finish=end;   //    therefore there is no need to adjust with a -1
        for(start=beg; start<=finish; start++){
            list.remove(beg);
        }
    }
}
